package edu.cis084.week5;

/*
 * 
 * 
*/
public class TemperatureConverter 
{
	// Store what we want to convert to and what scale the temp is currently in.
	private String currentScale;
	// Store what the actual numerical temp. value is.
	private float temperatureValue;
	
	public TemperatureConverter()
	{
		currentScale = "c";
		temperatureValue = 0;
	}
	
	public TemperatureConverter(String currScale)
	{
		currentScale = currScale.toLowerCase();
		temperatureValue = 0;
	}
	
	public TemperatureConverter(float currTemp)
	{
		currentScale = "c";
		temperatureValue = currTemp;
	}
	
	/*************************
	 * Set Methods           *
	 *************************/
	
	public void setTemperatureValue(float tempValue)
	{
		temperatureValue = tempValue;
	}
	
	public void setTemperatureScale(String newScale) throws InvalidScaleException
	{
		switch(newScale.toLowerCase())
		{
			case "c":
			case "f":
			case "k":
				currentScale = newScale.toLowerCase();
				break;
			default:
				throw new InvalidScaleException();
		}
	}
	
	/*************************
	 *  Get Methods          *
	 *************************/
	
	public float getTemperatureValue()
	{
		return temperatureValue;
	}
	
	public String getCurrentScale()
	{
		return currentScale;
	}
	
	public float getTempInFahrenheit()
	{
		float conversionResult;
		
		if(currentScale.equals("c"))
			conversionResult = (temperatureValue*(9/5)+32);
		else if(currentScale.equals("k"))
			conversionResult = (float) (temperatureValue * (9/5) - 459.67);
		else
			conversionResult = temperatureValue;
		
		return conversionResult;
	}
	
	public float getTempInCelsius()
	{
		float conversionResult;
		
		if(currentScale.equals("f"))
			conversionResult = 5 * (temperatureValue - 32) / 9;
		else if(currentScale.equals("k"))
			conversionResult = (float) (temperatureValue - 273.15);
		else
			conversionResult = temperatureValue;
		
		return conversionResult;
	}
	
	public float getTempInKelvin()
	{
		float conversionResult;
		
		if(currentScale.equals("c"))
			conversionResult = (float) (temperatureValue + 273.15);
		else if(currentScale.equals("f"))
			conversionResult = (float) ((temperatureValue + 459.67) * (5/9));
		else
			conversionResult = temperatureValue;
		
		return conversionResult;
	}
	
	/*********************
	 *  Instance Methods *
	 *********************/
	
	
	public float convertTo(String convertTo) throws InvalidScaleException
	{
		float convertedValue = 0;
		
		switch(convertTo)
		{
			case "c":
				convertedValue = this.getTempInCelsius();
				break;
			case "f":
				convertedValue = this.getTempInFahrenheit();
				break;
			case "k":
				convertedValue = this.getTempInKelvin();
				break;
			default:
				throw new InvalidScaleException();
		}
		
		return convertedValue;
	}
	
	public boolean equals(TemperatureConverter compareTemp) throws InvalidScaleException
	{
		if(compareTemp.getCurrentScale().equals(this.getCurrentScale()))
		{
			if(compareTemp.getTemperatureValue() == this.getTemperatureValue())
				return true;
			else
				return false;
		}
		else
		{
			if(compareTemp.convertTo(this.getCurrentScale())==this.getTemperatureValue())
				return true;
			else
				return false;
		}
	}
	
	public boolean greaterThan(TemperatureConverter compareTemp) throws InvalidScaleException
	{
		if(compareTemp.getCurrentScale().equals(this.getCurrentScale()))
		{
			if(compareTemp.getTemperatureValue() < this.getTemperatureValue())
				return true;
			else
				return false;
		}
		else
		{
			if(compareTemp.convertTo(this.getCurrentScale()) < this.getTemperatureValue())
				return true;
			else
				return false;
		}
	}
	
	public boolean lessThan(TemperatureConverter compareTemp) throws InvalidScaleException
	{
		if(compareTemp.getCurrentScale().equals(this.getCurrentScale()))
		{
			if(compareTemp.getTemperatureValue() > this.getTemperatureValue())
				return true;
			else
				return false;
		}
		else
		{
			if(compareTemp.convertTo(this.getCurrentScale()) > this.getTemperatureValue())
				return true;
			else
				return false;
		}
	}
	
	public String toString()
	{
		return temperatureValue + currentScale.toUpperCase();
	}
}
